package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.Constants;
import com.tanhua.dubbo.api.db.UserApi;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.model.db.User;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessageService {

    @DubboReference
    private UserApi userApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @DubboReference
    private FriendApi friendApi;

    public UserInfoVo getUserInfoByHuanxinId(String huanxinId) {
        User user = new User();
        user.setHxUser(huanxinId);
        user = userApi.find(user);
        UserInfo userInfo = userInfoApi.getById(user.getId());
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, vo);
        vo.setAge(userInfo.getAge().toString());
        return vo;
    }

    /**
     * 添加朋友
     * @param params
     */
    public void makeFriends(Map<String, String> params) {
        Long userId = Long.valueOf(params.get("userId"));
        //环信添加好友
        huanXinTemplate.addContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(),
                Constants.HX_USER_PREFIX + userId);
        //数据库添加好友
        friendApi.makeFriends(UserHolder.getUserId(), userId);
    }

    public PageResult<ContactVo> contactList(Long page, Long pageSize, String keyword) {
        PageResult<Friend> pageResult = friendApi.getList(page, pageSize, UserHolder.getUserId());
        List<Friend> friends = pageResult.getItems();
        List<ContactVo> contactVos = new ArrayList<>();
        if(!CollectionUtils.isEmpty(friends)) {
            List<Long> userIds = CollUtil.getFieldValues(friends, "friendId", Long.class);
            UserInfo user = new UserInfo();
            user.setNickname(keyword);
            Map<Long, UserInfo> userInfos = userInfoApi.getBatchIdsByCondition(userIds, user);

            if(!CollectionUtils.isEmpty(userInfos)){
                friends.forEach(friend -> {
                    UserInfo userInfo = userInfos.get(friend.getFriendId());
                    if(null != userInfo)
                        contactVos.add(ContactVo.init(userInfo));
                });
            }
        }

        return new PageResult<>(page, pageSize, pageResult.getCounts(),contactVos);
    }
}
